<?php
	include_once("global_modules.php");
	
	/**
	 * This function returns the ldap pendencies.
	 * @return Array containing the data of ldap pendency.
	 */
	function get_ldap_pendencies(){
		$connection = database_connect();
		
		$query = database_query($connection, "SELECT * FROM ldap_pendency");

		$pendencies_list = array();
		$row = 0;
		while($results = database_fetch_array($query, $row)){
			$pendencies_list[] = $results;
			$row++;
		}
		
		database_close($connection);
		
		return $pendencies_list;
	}
	
	/**
	 * This function resolves the LDAP pendency, ie, try to execute the pendency in the LDAP server.
	 * @param $pendency_id Integer containing the pendency id.
	 * @return Boolean True if the replication was successfully, of FALSE when not.
	 */
	function resolve_ldap_pendecy($pendency_id){
		$connection = database_connect();
		
		$ldap_host = null;
		$ldap_port = null;
		$ldap_user = null;
		$ldap_pass = null;
		$ldap_tree = null;
		
		$query = database_query($connection, "SELECT * FROM configurations");
		
		$row = 0;
		while($results = database_fetch_object($query, $row)){
			switch($results->name){
				case "LDAP_HOST":
					$ldap_host = $results->value;
					break;
				case "LDAP_PORT":
					$ldap_port = $results->value;
					break;
				case "LDAP_USER_TO_BIND":
					$ldap_user = $results->value;
					break;
				case "LDAP_PASS_TO_BIND":
					$ldap_pass = $results->value;
					break;
				case "LDAP_TREE":
					$ldap_tree = $results->value;
					break;
			}
			$row++;
		}
		
		$query = database_query($connection, "SELECT * FROM ldap_pendency WHERE id = ".$pendency_id."");
		$pendency_data = database_fetch_object($query);
		
		# LDAP functions
		$ldap_connection = ldap_connect($ldap_host, $ldap_port);
		ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3);
		ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0);
		
		if($ldap_connection){
			$ldap_bind = ldap_bind($ldap_connection, $ldap_user, $ldap_pass);
			
			$enable = ($pendency_data->enable == 't') ? "TRUE" : "FALSE";
			$admin = ($pendency_data->admin == 't') ? "TRUE" : "FALSE";
			
			$data = array('cn' => array(substr($pendency_data->username, 0, strpos($pendency_data->username, '@'))),
						  'gidnumber' => array($pendency_data->gidnumber),
						  'homedirectory' => array($pendency_data->homedirectory),
						  'sn' => array($pendency_data->name),
						  'uidnumber' => array($pendency_data->user_id),
						  'uid' => array($pendency_data->username),
						  'eduPersonPrincipalName' => array($pendency_data->username),
						  'mail' => array($pendency_data->email),
						  'userpassword' => array($pendency_data->password),
						  'loginShell' => array("/bin/bash"),
						  'userEnable' => array($enable),
						  'omfAdmin' => array($admin)
						 );
						 
			$ldap_user_node = "uid=".$pendency_data->username.",ou=people,".$ldap_tree."";
			
			$resolve_pendency = false;
			switch($pendency_data->action){
				case "ADD":
					$data['objectClass'] = array('top','person','inetOrgPerson','posixAccount','eduPerson','brPerson','schacPersonalCharacteristics','fibre');
					
					$resolve_pendency = ldap_add($ldap_connection, $ldap_user_node, $data);
					break;
				case "DEL":
					$resolve_pendency = ldap_delete($ldap_connection, $ldap_user_node);
					break;
				case "MODIFY":
					$resolve_pendency = ldap_modify($ldap_connection, $ldap_user_node, $data);
					break;
			}
		}else
			return FALSE;
		
		if($resolve_pendency){
			$query = "DELETE FROM ldap_pendency WHERE id = ".$pendency_id."";
			$remove_pendency = database_query($connection, $query);
			
			database_close($connection);
			
			if($remove_pendency)
				return TRUE;
			else
				return FALSE;
		}else
			return FALSE;
	}
?>
